#include "LongReadInBits.h"

unsigned int encodeLongRead(const char* read, CReadInBits& firstHalf, CReadInBits& secondHalf)
{
    int readLength = (int)strlen(read);
    int secondHalfStart = readLength - CReadInBits::iReadLength;
    if (read != NULL && readLength > (int)MAX_READ_LENGTH) {
        secondHalf.encode(&read[secondHalfStart]);
        firstHalf.encode(read);
        return(0); // Potential error for buffer overflow
    } else {
        return(1);
    }
}

unsigned int encodeRead(const char* read, CLongReadInBits& r)
{
    return(encodeLongRead(read, r.first, r.second));
}

// Reverse compliment ACGT long read (in base)

inline bool canBeEncoded(unsigned int readLength)
{
    return(readLength > MAX_READ_LENGTH &&  readLength <= MAX_LONG_READ_LENGTH);
}

CLongReadInBits reverseCompliment(CLongReadInBits r, unsigned int uiReadLength)   // must pass by value
{
    CLongReadInBits reverseComplementRead;
    if(canBeEncoded(uiReadLength)) {
        unsigned int halfReadLength = (uiReadLength + 1)/2;
        reverseComplementRead.first = reverseCompliment(r.second, halfReadLength);
        reverseComplementRead.second = reverseCompliment(r.first, halfReadLength);
    }
    return(reverseComplementRead);
}
